/**
 * @fileOverview Model modul of Counter widget MVC
 * 
 * @author Adam Hořčica horcicaa@gmail.com
 * @url $HeadURL: http://ahs-gadgets.googlecode.com/svn/trunk/bb-counters/src/counter_model.js $
 * @revision $Id: counter_model.js 47 2008-02-09 20:30:13Z horcicaa $
 */

/**
 * Model modul of Counter widget MVC (for XML API)
 *  
 * @constructor
 * @param {CounterController} controller Controller of widget
 * @param {number} id Counter ID      
 */ 
function CounterModel(controller, id)
{
  /** Widget controller */
  this.controller = controller;
  
  /** Counter ID */
  this.id = id;
  /** Couter data */
  this.data = {}; 
}

/** Blueboard.cz Counter API adress */
CounterModel.BB_API_PATH = "http://www.blueboard.cz/api/counter.php?output=xml&id=";

/** Blueboard.cz public statistic adress */
CounterView.STAT_LINK = "http://www.blueboard.cz/pocitadla-statistiky.php?id=";

CounterModel.prototype = {

  /**
   * Send request for counter data
   * Use XML data API.   
   */     
  requestForData: function()
  {
    var url = CounterModel.BB_API_PATH + this.id;
    var that = this;

    /** @ignore */
    function dataCallback(xml)
    {
      if (xml == null || typeof(xml) != "object" || xml.firstChild == null) 
      {
        that.controller.showError("__MSG_wrong_data_text__");
        return;
      }
      
      try
      {
        var root = xml.documentElement;
        var id = _trim(root.getAttribute("id"));
        
        if(root.tagName != "counter" && that.id != id)
        {
          that.controller.showError("__MSG_wrong_data_text__");
          return;
        }
        
        /** @ignore */
        function getText(oNode) {
          var sText = "";
          for (var i = 0; i < oNode.childNodes.length; i++) {
            if (oNode.childNodes[i].hasChildNodes()) {
              sText += getText(oNode.childNodes[i]);
            } else {
              sText += oNode.childNodes[i].nodeValue;
            }
          }
          return sText;
        }
        
        var data = that.data;
        
        data.id = id;
        data.name = _trim(getText(root.getElementsByTagName("name")[0]));
        if(data.name == "")
        {
          data.name = that.controller.name; // Default name
        }
        
        data.link = CounterView.STAT_LINK + id;
        
        data.total = _trim(getText(root.getElementsByTagName("total")[0]));
        
        function detail(id)
        {
          var section = root.getElementsByTagName(id)[0];
          data[id] = {};
          data[id].visits = _trim(getText(section.getElementsByTagName("visits")[0]));
          data[id].pageviews = _trim(getText(section.getElementsByTagName("pageviews")[0]));
        }
        
        detail("today");
        detail("yesterday");
        detail("month");
        
        that.controller.updateData();
        return;
      }
      catch(e)
      {
        that.controller.showError("__MSG_wrong_data_text__");
        return;
      }
    }
    
    _IG_FetchXmlContent(url, dataCallback);

  },
  
  /**
   * Retirn received counter data.
   * @return {data_object}   
   */     
  getData: function()
  {
    return this.data;
  }
  
};
